package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    //添加购物车
    @PostMapping("/add")
    public R save(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
    /* 菜品或者套餐是否点过
     如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
     如果是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
     如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
     */
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);

        Long dishId = shoppingCart.getDishId();
        if (null != dishId) {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);

        if (one == null) {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        } else {
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }

    //查询购物车
    @GetMapping("/list")
    public R list(HttpSession session) {

        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart, HttpSession session) {

        //菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if (null != dishId) {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number>0){
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.remove(qw);
        }
        return R.success(one);

    }



    @DeleteMapping("/clean")
    public R clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");

        shoppingCartService.removeById(userId);
        return R.success("清空购物车成功");
    }


}
